package com.sxkiler.demo.test.data;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.sxkiler.demo.test.entity.ESDocCount;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户健康度检查
 */
public class UserHealthCheck {

    String path="D:\\work_docs\\数据分析\\客户健康度\\20210910\\";

    public Map<String,Integer> getLoginReocrd(List<String> loginRecord){
        Map resultMap = new HashMap();
        for(String record:loginRecord){
            try{
                String [] values = record.split(",");
                String fecid = getValueString(values[0]);
                String lgCount = getValueString(values[1]);
                System.out.println("fecid="+fecid+",lgcount="+lgCount);
                resultMap.put(fecid,StrUtil.isEmpty(lgCount)?0:Integer.valueOf(lgCount));
            }catch (Exception e){e.printStackTrace();}
        }
        return resultMap;
    }



    @Test
    public void test(){
        String jsonStr = FileUtil.readString(new File(path + "oper.json"),"UTF-8");
        List<String> lines = FileUtil.readLines(new File(path + "enterprise.csv"),"UTF-8");
        List<String> loginrecords = FileUtil.readLines(new File(path + "loginrecord.csv"),"UTF-8");
        Map<String,Integer> loginEc = getLoginReocrd(loginrecords);
//        System.out.println(jsonStr);
        String [] titles = new String[]{"fid","客户名称","片区","续费月","应签日期","注册时间","首次下单时间","租赁年限","当前在用项目数","登陆次数","总提单数","已关闭提单","未关闭提单"};
        List<ESDocCount> countList = JSON.parseArray(jsonStr,ESDocCount.class);
        Map<String,String> ecDocCount = countList.stream().collect(Collectors.toMap(o -> o.getKey(), o->o.getDoc_count()));
        System.out.println(countList.size());

        List<UserHealthVo> healthList = new ArrayList<>();
        healthList.add(new UserHealthVo());
        for(int i=1;i<lines.size();i++){
            String [] values = lines.get(i).split(",");
            UserHealthVo vo = new UserHealthVo(values);
            String docCount = ecDocCount.get(vo.getId());
            if(StrUtil.isNotEmpty(docCount)){
                vo.setOperCount(Integer.parseInt(docCount));
                vo.isOper = "是";
            }else{
                vo.setOperCount(0);
                vo.isOper = "否";
            }
            if(loginEc.get(vo.getId())==null){
                vo.setLoginCount(0);
            }else{
                vo.setLoginCount(loginEc.get(vo.getId()));
            }
            if(StrUtil.compare(vo.getEndDate(),"2020-12-31",true)>0
                && !("是".equalsIgnoreCase(vo.isOper) && "是".equalsIgnoreCase(vo.isLogin))){
                healthList.add(vo);
            }
        }
        healthList.sort(new Comparator<UserHealthVo>() {
            @Override
            public int compare(UserHealthVo o1, UserHealthVo o2) {
                String endDate1 = o1.getEndDate();
                String endDate2 = o2.getEndDate();
                if(o2.getEndDate().equalsIgnoreCase("应签日期")){
                    return 1;
                }
                if(DateUtil.parse(endDate2).compareTo(DateUtil.parseDate(endDate1))>0){
                    return 1;
                }else{
                    return -1;
                }
            }
        });
        System.out.println(healthList.size());
        FileUtil.writeLines(healthList,path + "result2.csv","UTF-8");

    }

    @Test
    public void testCompare(){
        System.out.println(StrUtil.compare("2020-01-01","2020-12-31",true));
    }
    public static String getValueString(String str){
        return  str.replaceAll("\"","");
    }
    public static String getValueString(String str,String defaultValue){
        String result = str.replaceAll("\"","");
        if(StrUtil.isNotEmpty(result)){
            return result;
        }
        return defaultValue;
    }
}
